import axios from '../../src/index';

// get 请求
// 类型一
axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: ['bar', 'baz']
  }
});

// 类型二
axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: {
      bar: 'baz'
    }
  }
});

// 类型三
const date = new Date();
axios({
  method: 'get',
  url: '/base/get',
  params: {
    date
  }
});

// 类型四
axios({
  method: 'get',
  url: '/base/get',
  params: {
    foo: 'bar,@:% & '
  }
});
// 类型五
axios({
  method: 'get',
  url: '/base/get',
  params: {
    bar: 'baz',
    foo: null
  }
});

// 类型六
axios({
  method: 'get',
  url: '/base/get#hash',
  params: {
    foo: 'bar'
  }
});

// 类型七
axios({
  method: 'get',
  url: '/base/get?foo=bar',
  params: {
    bar: 'baz'
  }
});


// post 请求
// 类型一
axios({
  method: 'post',
  url: '/base/post',
  data: {
    name: 'Tom',
    age: 23
  }
});

// 类型二
const arr = new Int32Array([21, 31]);
axios({
  method: 'post',
  url: '/base/buffer',
  data: arr
});

// 类型三
// post 请求配置 headers, 解决上面类型一返回为空对象的问题
axios({
  method: 'post',
  headers: {
    'content-type': 'application/json',
    'Accept': 'application/json, text/plain, */*'
  },
  url: '/base/post',
  data: {
    name: 'Tom',
    age: 23
  }
});

// 类型四
// Content-Type: application/x-www-form-urlencoded;charset=UTF-8
// formData的类型
const paramsStrng = 'q=URLUtils.searchParams&topic=api';
const searchParams = new URLSearchParams(paramsStrng);
axios({
  method: 'post',
  url: '/base/post',
  data: searchParams
});


// Promise 接收后端返回的数据
axios({
  method: 'post',
  url: '/base/post',
  data: {
    name: 'Tom222',
    age: 2322
  }
}).then(res => {
  console.log(res)
});

axios({
  method: 'post',
  url: '/base/post',
  data: {
    name: 'Tom',
    age: 23
  },
  responseType: 'json'
}).then(res => {
  console.log(res)
});
